package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.dao.CheckItemDao;
import com.entity.PageResult;
import com.entity.QueryPageBean;
import com.entity.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pojo.CheckItem;
import com.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service(interfaceClass = com.service.CheckItemService.class)
@Transactional//TODO 此处不好用
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    //查所有
    @Override
    public List<CheckItem> findAllCheckTtem() {
        return checkItemDao.findAllCheckTtem();
    }

    //新增
    @Override
    public Result insertCheckTtem(CheckItem checkItem) {
        Result result;
        //校验数据
        String name = checkItem.getName();
        String code = checkItem.getCode();
        String age = checkItem.getAge();
        Float price = checkItem.getPrice();
        String sex = checkItem.getSex();
        if (name != null && code != null && age != null && price != null && sex != null) {
            if ("".equals(name) && "".equals(code) && "".equals(age) && "".equals(sex)) {
                return new Result(false, "新增数据不能为空");
            } else {
                String a1 = "^\\d{4}$";
                boolean b1 = code.matches(a1);
                if (!b1) {
                    return new Result(false, "项目编码格式错误");
                }
                if (name.length() > 32) {
                    return new Result(false, "项目名称过长");
                }
                String a2 = "^[0][-](?:[1-9][0-9]?|1[01][0-9]|100)$";
                boolean b2 = age.matches(a2);
                if (!b2) {
                    return new Result(false, "适用年纪有误");
                }
                String a3 = "^((\\d+\\.\\d*[1-9]\\d*)|(\\d*[1-9]\\d*\\.\\d+)|(\\d*[1-9]\\d*))$";
                String a33 = price.toString();
                boolean b3 = a33.matches(a3);
                if (!b3) {
                    return new Result(false, "价钱格式有误");
                }
            }
        } else {
            return new Result(false, "新增数据不能为空");
        }
        //校验通过
        int i = checkItemDao.insertCheckTtem(checkItem);
        if (i != 0) {
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } else {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    //分页查询
    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        Integer cp = queryPageBean.getCurrentPage();
        Integer p = queryPageBean.getPageSize();
        PageHelper.startPage(cp, p);
        Page<CheckItem> page = checkItemDao.findByPage(queryPageBean.getQueryString());
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total, rows);
    }

    //删除检查项
    @Override
    public Result deleteCheckItem(int id) {
        //校验
        if (id <= 0) {
            return new Result(false, "删除失败");
        }
        int i = checkItemDao.deletegroupitemById(id);
        int i2 = checkItemDao.deleteCheckItem(id);
        if (i2 != 0) {
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } else {
            return new Result(false, "删除的内容不存在");
        }
    }

    //动态修改检查项数据数据
    @Override
    public Result updateCheckItem(CheckItem checkItem) {
        //校验数据有无问题
        String name = checkItem.getName();
        String code = checkItem.getCode();
        String age = checkItem.getAge();
        Float price = checkItem.getPrice();
        String sex = checkItem.getSex();
        /*if (name != null && code != null && age != null && price != null && sex != null) {
            if ("".equals(name) && "".equals(code) && "".equals(age) && "".equals(sex)) {
                return new Result(false, "新增数据不能为空");
            } else {
                String a1 = "^\\d{4}$";
                boolean b1 = code.matches(a1);
                if (!b1) {
                    return new Result(false, "项目编码格式错误");
                }
                if (name.length() > 32) {
                    return new Result(false, "项目名称过长");
                }
                String a2 = "^[0][-](?:[1-9][0-9]?|1[01][0-9]|100)$";
                boolean b2 = age.matches(a2);
                if (!b2) {
                    return new Result(false, "适用年纪有误");
                }
                String a3 = "^((\\d+\\.\\d*[1-9]\\d*)|(\\d*[1-9]\\d*\\.\\d+)|(\\d*[1-9]\\d*))$";
                String a33 = price.toString();
                boolean b3 = a33.matches(a3);
                if (!b3) {
                    return new Result(false, "价钱格式有误");
                }
            }
        } else {
            return new Result(false, "新增数据不能为空");
        }*/
        //校验通过
        int i = checkItemDao.updateCheckItem(checkItem);
        if (i != 0) {
            //成功
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } else {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
}
